//
//  Kylin_AdViewController.m
//  03-BuDeJie
//
//  Created by Kylin on 16/2/24.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_AdViewController.h"
#import "Kylin_TabBarController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "Kylin_AdItem.h"
#define Kylin_Code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
@interface Kylin_AdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImage;

@property (weak, nonatomic) IBOutlet UIView *AdView;
@property (weak, nonatomic) IBOutlet UIButton *jumpButton;

@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, weak) UIImageView *adImageView;
/**item*/
@property(nonatomic,strong)Kylin_AdItem *item;
@end

@implementation Kylin_AdViewController

- (UIImageView *)adImageView{
    
    if (!_adImageView) {
        
        UIImageView *adImageView = [[UIImageView alloc]init];
        [_AdView addSubview:adImageView];
        _adImageView = adImageView;
        
        // 添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_adImageView addGestureRecognizer:tap];
        
        _adImageView.userInteractionEnabled = YES;
    }
    return _adImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLaunchImage];
    [self loadData];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}


- (void)timeChange{
    
    static NSInteger index = 3;
    if (index == -1) {
        [self jumpButtonClick];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"跳过(%zd)",index];
    [self.jumpButton setTitle:str forState:UIControlStateNormal];
    index --;
}
- (void)tap{
    NSURL *url = [NSURL URLWithString:self.item.ori_curl];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        [[UIApplication sharedApplication]openURL:url];
    }
}

- (IBAction)jumpButtonClick {
    
    Kylin_TabBarController *tabBarVC = [[Kylin_TabBarController alloc]init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
    
    [self.timer invalidate];
}
- (void)loadData{
    
    AFHTTPSessionManager *manager =  [AFHTTPSessionManager manager];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    param[@"code2"] = Kylin_Code2;
    
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 获取AD字典
        NSDictionary *adDict = [responseObject[@"ad"] lastObject];
        
        self.item = [Kylin_AdItem mj_objectWithKeyValues:adDict];
        
        // 展示广告图片
        CGFloat adW = Kylin_ScreenW;
        if (_item.h <= 0) return;
        CGFloat adH = Kylin_ScreenW / _item.w * _item.h;
        if (adH > 557) {
            adH = 557;
        }
        
        // 开发习惯:每次创建控件,一定要考虑要创建多少次,只要创建一次,采用懒加载
        self.adImageView.frame = CGRectMake(0, 0, adW, adH);
        
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:self.item.w_picurl]];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Kylin_LogFunc;
    }];
    
    
    
}
- (void)setupLaunchImage{
    if (iPhone6P) { // 6 plus
        _launchImage.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iPhone6) {
        _launchImage.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iPhone5) {
        _launchImage.image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    } else if (iPhone4) {
        _launchImage.image = [UIImage imageNamed:@"LaunchImage-700"];
    }
}
@end
